<?php

/*
 * 
 */

include_once dirname(__FILE__)."/basedao.php";
include_once dirname(__FILE__)."/../objects/question.php";

class QuestionsDAO extends BaseDAO {
	/**
	 * 添加试题
	 * @param type $question
	 */
	public function insertQuestion($question){		
		$this->getConn()->query("LOCK TABLES questions WRITE");
		$sqlstr = "INSERT INTO questions(question_title, question_answer_a, question_answer_b, question_answer_c, question_answer_d, question_answer_correct, question_analytic, question_chapter_id) VALUES(?,?,?,?,?,?,?,?)";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("sssssisi",
				$question->getQuestionTitle(),
				$question->getQuestionAnswerA(),
				$question->getQuestionAnswerB(),
				$question->getQuestionAnswerC(),
				$question->getQuestionAnswerD(),
				$question->getQuestionAnswerCorrect(),
				$question->getQuestionAnalytic(),
				$question->getQuestionChapterId());
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}	
	/**
	 * 删除试题
	 * @param type $question_id
	 */
	public function deleteQuestion($question_id){
		$this->getConn()->query("LOCK TABLES questions WRITE");
		$sqlstr = "DELETE FROM questions WHERE question_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i",
				$question_id);
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}
	/**
	 * 取得所有试题
	 * @return array
	 */
	public function getAllQuestions(){
		$questions = array();
		$this->getConn()->query("LOCK TABLES questions READ");
		$sqlstr = "SELECT question_id, question_title, question_answer_a, question_answer_b, question_answer_c, question_answer_d, question_answer_correct, question_analytic, question_chapter_id FROM questions";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->execute();
		$stmt->bind_result($question_id, $question_title, $question_answer_a, $question_answer_b, $question_answer_c, $question_answer_d, $question_answer_correct, $question_analytic, $question_chapter_id);
		while($stmt->fetch()){
			$question = new Question();
			$question->setQuestionId($question_id);
			$question->setQuestionTitle($question_title);
			$question->setQuestionAnswerA($question_answer_a);
			$question->setQuestionAnswerB($question_answer_b);
			$question->setQuestionAnswerC($question_answer_c);
			$question->setQuestionAnswerD($question_answer_d);
			$question->setQuestionAnswerCorrect($question_answer_correct);
			$question->setQuestionAnalytic($question_analytic);
			$question->setQuestionChapterId($question_chapter_id);
			array_push($questions, $question);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $questions;	
	}
	/**
	 * 取得某章节的所有试题
	 * @param type $chapter_id
	 * @return array
	 */
	public function getQuestionsByChapter($question_chapter_id){
		$questions = array();
		$this->getConn()->query("LOCK TABLES questions READ");
		$sqlstr = "SELECT question_id, question_title, question_answer_a, question_answer_b, question_answer_c, question_answer_d, question_answer_correct, question_analytic, question_chapter_id FROM questions WHERE question_chapter_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i",
				$question_chapter_id);
		$stmt->execute();
		$stmt->bind_result($question_id, $question_title, $question_answer_a, $question_answer_b, $question_answer_c, $question_answer_d, $question_answer_correct, $question_analytic, $question_chapter_id);
		while($stmt->fetch()){
			$question = new Question();
			$question->setQuestionId($question_id);
			$question->setQuestionTitle($question_title);
			$question->setQuestionAnswerA($question_answer_a);
			$question->setQuestionAnswerB($question_answer_b);
			$question->setQuestionAnswerC($question_answer_c);
			$question->setQuestionAnswerD($question_answer_d);
			$question->setQuestionAnswerCorrect($question_answer_correct);
			$question->setQuestionAnalytic($question_analytic);
			$question->setQuestionChapterId($question_chapter_id);
			array_push($questions, $question);
		}
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		return $questions;	
	}
}

?>
